package com.slofzx.nuoda.evaluation.version.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;

/**
 * 系统读写文件工具类
 * @author sunfuyong
 */
@Component
public class CustomFileUtil {
    private Logger logger = LoggerFactory.getLogger(CustomFileUtil.class);

    /**
     * 计算日志保存路径
     */
//    @Value("${custom.path.log}")
    private String logPath;

    /**
     * 计算结果缓存路径
     */
//    @Value("${custom.path.download}")
    private String downloadPath;
    /**
     * 模板上传路径
     */
//    @Value("${custom.path.upload}")
    private String uploadPath;

    @Value("${custom.path.download}")
    public void setDownloadPath(String downloadPath) {
        this.downloadPath = downloadPath;
        createDirIfNotExists(downloadPath);
    }

    @Value("${custom.path.upload}")
    public void setUploadPath(String uploadPath) {
        this.uploadPath = uploadPath;
        createDirIfNotExists(uploadPath);
    }

    @Value("${custom.path.log}")
    public void setLogPath(String logPath) {
        this.logPath = logPath;
        createDirIfNotExists(logPath);
    }

    /**
     * 创建不存在目录
     * @param path
     */
    private void createDirIfNotExists(String path) {
        File dir = new File(path);
        if(!dir.exists()){
            boolean result = dir.mkdirs();
            logger.info("创建目录 {} {}",path,result?"成功":"失败");

        }
    }
    /**
     * 获取计算日志文件
     * @param id
     * @return
     */
    public File getComputeLogFile(String id){
        File file = new File(logPath+"compute-"+id+".log");
        return file;
    }

    /**
     * 删除计算日志文件
     * @param id
     * @return
     */
    public boolean deleteComputeLogFile(String id) {
        File file = getComputeLogFile(id);
        if(file.exists()) {
            return file.delete();
        }
        return true;
    }

    /**
     * 获取缓存的计算结果文件
     * @param dataVersion
     * @return
     */
    public File getComputeResultCacheFile(String dataVersion) {
        String cachePath = downloadPath+dataVersion+".xlsx";
        File file = new File(cachePath);
        return file;
    }
    /**
     * 获取缓存的计算结果文件
     * @param dataVersion
     * @return
     */
    public boolean deleteComputeResultCacheFile(String dataVersion) {
        File file = getComputeResultCacheFile(dataVersion);
        if(file.exists()) {
            return file.delete();
        }
        return true;
    }

    /**
     * 获取评价模型模板文件
     * @param modelId
     * @return
     */
    public File getUploadTemplateFile(String modelId){
        File file = new File(uploadPath+modelId+".template");
        return file;
    }

//    /**
//     * 删除评价模型模板文件
//     * @param modelId
//     * @return
//     */
//    public boolean deleteUploadTemplateFile(String modelId){
//        File file = getUploadTemplateFile(modelId);
//        if(file.exists()) {
//            return file.delete();
//        }
//
//        return true;
//    }

    public String getUploadPath() {
        return uploadPath;
    }
}
